TOKENS:
They are the smallest individual unit that we give in the program.
They are
Keywords
Identifiers
Constants/literals
StringsOperators
Keywords:
The meanings of keywords have been set as fixed and cannot be altered.
In C, there are 32 keywords in all.
Lowercase letters are used to write keywords.
For example, int, double.
Identifiers
- An underscore or a letter must be the first character.
- It must be formed by underscore, numeral or letter.
- The identifier must not be a keyword.
- It does not contain any blank or white space characters.
- It should be meaningful.
Integer Constants
All that an integer constant is is a value made up of digits or numbers. For instance, 111, 1234
Floating point Constants
Constants that have a fraction value or a decimal point in them. For instance, 223.14, 400.054
Character Constants
A character constant is made up of just one character and one quote (' '). Such as "A," "m," and "9."
Strings
All strings are is a character array terminated with the null character ('\0').
Double quotes are always used to enclose strings.
Operators:
The operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations
Example: +, -, /, *, ++, --
Types of ‘C’ operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic Operators
Arithmetic operators are used for performing basic mathematical operations on operands.
Operator | Name | Description | Example |
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
-- | Decrement | Decreases the value of a variable by 1 | --x |
Example Program
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4;
printf("%d ", ++a); //pre-increment //10
printf("\n %d ", a++); //post increment 10
printf("\n %d ", a); //11
printf("\n a+b = %d \n",a+b); //11+4
printf("a-b = %d \n",a-b); //11-4
printf("a*b = %d \n",a*b); //11*4
printf("a/b = %d \n",a/b); //11/4 (quotient)
printf("Remainder when a divided by b = %d \n",a%b); //11%4 (remanider)
printf("%d ", --b); //4
printf("%d ", b--);
printf("%d ", b);
return 0;
}
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a,b,c,d;
a=30;
b=100;
c=40;
d=26;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("c++ = %d \n", c++);
printf("%d \n", c);
printf(" --d = %d \n", d--);
printf("%d", d);
return 0;
}
Relational Operators
Relational or comparison operators are used to compare two operands.
The result of the evaluation is either true or false.
Operator | Name | Example |
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Example Program
// Working of relational operators true = 1 false = 0
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b); //1
printf("%d == %d is %d \n", a, c, a == c); //0
printf("%d > %d is %d \n", a, b, a > b); //0
printf("%d > %d is %d \n", a, c, a > c); //0
printf("%d < %d is %d \n", a, b, a < b); //0
printf("%d < %d is %d \n", a, c, a < c); //1
printf("%d != %d is %d \n", a, b, a != b); //0
printf("%d != %d is %d \n", a, c, a != c); //1
printf("%d >= %d is %d \n", a, b, a >= b); //1
printf("%d >= %d is %d \n", a, c, a >= c); //0
printf("%d <= %d is %d \n", a, b, a <= b); //1
printf("%d <= %d is %d \n", a, c, a <= c); //1
return 0;
}
Logical Operators
«Logical operators are used for evaluating a combination of conditions/constraints
to get a resultant value.
«The result of the evaluation of a Boolean expression is Boolean which is either
true or false.
Operator | Name | Description | Example |
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
This is the simplest assignment operator.
This operator is used to assign the
Operator | Example | Same As |
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
Example Program:
// Working of assignment operator =
#include <stdio.h>
int main()
{
int a = 5, c=20;
c += a; //c=c+a; 25
printf("c = %d\n", c);
c -= a; //20
printf("c = %d\n", c);
c *= a; //100
printf("c = %d\n", c);
c /= a; //20
printf("c = %d\n", c);
c %= a; //0
printf("c = %d\n", c);
return 0;
}
Bitwise Operators
In C, bitwise operators perform operations on integer data at the
individual bit-level.
& (bitwise and)
| (bitwise or)
<< (bitwise shift left)
>> (bitwise shift right)
Example Program:
/*Conditional operator
syntax ?:(if else replacement)*/
#include<stdio.h>
int main() {
printf("\tA program to check whether the given number is even number or odd number\n");
int number;
printf("Enter the number: ");
scanf("%d",&number);
printf((number%2==0)?"\tThe given number is even":"\tThe given number is odd");
}
Punctuators
(,) Comma - Separator
(.) full stop - used to call a function
() Parentheses - defines a function
Curly braces {} - Specifies a block
(*) asterisk - Pointer
0 Comments